package com.xingyi.bacs.service;

import com.xingyi.bacs.bean.Bill;
import com.xingyi.bacs.bean.Merchant;
import com.xingyi.bacs.bean.Settlement;
import com.xingyi.bacs.dao.QsDao;
import com.xingyi.bacs.mapper.QsMapper;
import com.xingyi.bacs.util.DesUtil;
import com.xingyi.bacs.util.LogUtil;
import com.xingyi.bacs.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class QsServiceImpl implements QsService{

    @Resource
    private QsMapper qsMapper;
    @Resource
    private QsDao qsDao;

    @Override
    public List<Merchant> runMerchant(String date) {
        List<Merchant> list = null;
        List<Merchant> mergeList = new ArrayList<Merchant>();
        try {
            list = qsMapper.queryMerchant(date);
            if (list!=null&&list.size()>0){
                for (Merchant data : list){
                    if (StringUtil.hasValue(data.getIdentity())){
                        data.setIdentity(DesUtil.encrypt(data.getIdentity(),DesUtil.KEY));
                    }
                    if (StringUtil.hasValue(data.getPhoneNumber())){
                        data.setPhoneNumber(DesUtil.encrypt(data.getPhoneNumber(),DesUtil.KEY));
                    }
                    mergeList.add(data);
                }
            }
            if (list!=null && list.size()>0){
                qsMapper.truncateMerchantTemp();
                qsDao.insertMerchantTemp(mergeList);
                qsMapper.mergeMerchant();
                LogUtil.info(QsServiceImpl.class,"本次商户表跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(QsServiceImpl.class,"本次商户表无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Settlement> runSettlement(String date) {
        List<Settlement> list = null;
        List<Settlement> mergeList = new ArrayList<Settlement>();
        try {
            list = qsMapper.querySettlement(date);
            if (list!=null&&list.size()>0){
                for (Settlement data : list){
                    if (StringUtil.hasValue(data.getIdentity())){
                        data.setIdentity(DesUtil.encrypt(data.getIdentity(),DesUtil.KEY));
                    }
                    if (StringUtil.hasValue(data.getPhoneNumber())){
                        data.setPhoneNumber(DesUtil.encrypt(data.getPhoneNumber(),DesUtil.KEY));
                    }
                    mergeList.add(data);
                }
            }
            if (list!=null && list.size()>0){
                qsDao.insertSettlement(mergeList);
                LogUtil.info(QsServiceImpl.class,"本次结算表跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(QsServiceImpl.class,"本次结算表无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Bill> runBill() {
        List<Bill> list = null;
        List<Bill> mergeList = new ArrayList<Bill>();
        try {
            list = qsMapper.queryBill();
            if (list!=null&&list.size()>0){
                for (Bill data : list){
                    if (StringUtil.hasValue(data.getPhoneNumber())){
                        data.setPhoneNumber(DesUtil.encrypt(data.getPhoneNumber(),DesUtil.KEY));
                    }
                    mergeList.add(data);
                }
            }
            qsMapper.truncateRunBill();
            if (list!=null && list.size()>0){
                qsDao.insertBill(mergeList);
                LogUtil.info(QsServiceImpl.class,"本次挂账表跑批数据量为 ："+list.size()+"条");
            }else{
                LogUtil.info(QsServiceImpl.class,"本次挂账表无数据进行跑批");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void deleteSettlement(String date) {
        try{
            qsMapper.deleteSettlement(date);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
